package com.lgd.tools;

import lombok.*;
import org.junit.Assert;
import org.junit.Test;

import java.text.Collator;
import java.util.*;

/**
 * @author 神隐`
 * @date 2019-04-22
 */


public class ListTest {

    @Test
    public void test1(){

        List<String> chinese = Arrays.asList("中国", "叶子", "天气", "李四");

        chinese.sort(Collator.getInstance(Locale.CHINA));
        System.out.println(chinese);

        ArrayList<Person> list = new ArrayList<>();

        int num = 80*10000;
        for (int i = 0; i < num; i++) {
            list.add(new Person("name"+i,i+18,i*10));
        }

        int sum =0;
        long start = System.currentTimeMillis();
        int size = list.size();
        for (int j=0;j< size;j++) {
            sum += list.get(j).getSalary();
        }
        long end = System.currentTimeMillis();

        System.out.println("forii:"+(end-start)+"ms");

        long start1 = System.currentTimeMillis();
        for (Person person : list) {
            sum += person.getSalary();
        }
        long end1 = System.currentTimeMillis();
        System.out.println("forr:"+(end1-start1)+"ms");

//        System.out.println(sum/ size);

    }

    @Test
    public void test2(){
        Person1 person1 = new Person1.Person1Builder().b1(true).age(12).isTime(false).build();
        System.out.println(person1.getB1());
        System.out.println(person1.isTime());

        Assert.assertEquals(person1.isTime(), person1.isTime);

        Assert.assertEquals(person1.isTime(),person1.time);

    }



    @Test
    public void sublistTest(){
        //使用sublist来处理列表的局部数据
        //使用sublist生成的list只是一个原list的一个视图.
        //所有对生成后的list的操作最终都会体现在原list上

        ArrayList<String> list = new ArrayList<>();

        list.add("test1");
        list.add("test2");
        list.add("test3");
        list.add("test4");
        list.add("test5");
        list.add("test6");
        list.add("test7");

        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        IntSummaryStatistics summary = integers.stream().mapToInt(t -> t).summaryStatistics();
        System.out.println(summary);

        list.forEach(System.out::println);

        List<String> list2 = list.subList(0, list.size());

        ArrayList<String> list3 = new ArrayList<>(list);

        System.out.println(list2);

        list2.add("test8");

        System.out.println(list.equals(list2));
        System.out.println(list.equals(list3));

        list.subList(1,2).clear();
        System.out.println(list);
    }
}

@Data
@AllArgsConstructor
@RequiredArgsConstructor
class Person {

    private String name;
    private Integer age;
    private Integer salary;

}



@Data
@AllArgsConstructor
@Builder
class Person1 {

    private String name;
    private Integer age;
    private Integer salary;
    private Boolean b1;

    @Getter
    private boolean b2;

    @Getter
    public boolean isTime = true;

    @Getter
    public boolean time = false;


}

